package com.gdut.sport;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.gdut.sport.adapter.HistoryListAdapter;
import com.gdut.sport.db.History;
import com.gdut.sport.db.HistoryDao;

import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {

    private ListView mHistoryListView;
    private HistoryListAdapter mListAdapter;
    private List<History> mHistoryList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        mHistoryListView = (ListView) findViewById(R.id.id_history_list);
        mHistoryListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                showChooseDialog(position);
                return true;
            }
        });
        fetchData();
    }

    private void showChooseDialog(final int pos) {
        String[] items = {"删除", "分享"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        delete(mHistoryList.get(pos));
                        update(pos);
                        break;
                    case 1:
                        share(mHistoryList.get(pos));
                        break;
                    default:
                        break;
                }
            }
        });
        builder.show();
    }

    private void delete(History history) {
        HistoryDao.delete(this, history);
    }

    private void update(int pos) {
        mHistoryList.remove(pos);
        mListAdapter.notifyDataSetChanged();
    }

    private void share(History history) {
        String text = "路程：" + history.getDistance() + "m\t" + "耗时：" + history.getTime();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
        intent.putExtra(Intent.EXTRA_TEXT, text);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, getTitle()));
    }

    private void fetchData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mHistoryList = HistoryDao.getHistoryList(HistoryActivity.this);
                if (mHistoryList == null) {
                    mHistoryList = new ArrayList<>();
                }
                mListAdapter = new HistoryListAdapter(HistoryActivity.this, mHistoryList);
                mHistoryListView.setAdapter(mListAdapter);
                new Handler(getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        mListAdapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();
    }
}
